package com.service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.mk.type.IntegerType;
import org.mk.type.Type;
import com.caucho.hessian.server.HessianServlet;
import com.db.connection.DBCPConnectionManager2;
import com.db.connection.DBCPPostgresManager;
import com.db.domain.Record;

public class SyncServiceImpl extends HessianServlet implements SyncService {

	public long clientToServer(long client, long id, Record record){
		long i = insert(client, id, record);
		
		return i;

	}

	/**
	 * @param i 客户端ID
	 * @return
	 */
	public static List<Record> returnToClient(long i) {
		List<Record>  aList = new ArrayList<Record>(); 
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
//        System.out.println("店"+i);
		try {
			
			conn = DBCPConnectionManager2.getConnection();
			// 执行SQL语句
			pstmt = conn.prepareStatement("select * from record where store_to = ? and flag = 'N' order by id");
			Type type = new IntegerType();
			type.nullSafeSet(pstmt, i, 1);			
			rs = pstmt.executeQuery();			
			if (rs.next()) {
				System.out.println("店"+i+"有数据");
				Record aRecord = new Record();
					long storeId = rs.getLong("store_to");
					String operation = rs.getString("operation");					
					String content = rs.getString("content");
					aRecord.content = content;			
					aRecord.setOperation(operation);
					aRecord.storeId = storeId;
					aRecord.id = rs.getLong("id");
					aRecord.storeFrom = rs.getLong("store_from");
					aList.add(aRecord);
				return aList;
			}
		} catch (Exception ex) {
			System.out.println("ErrorServer : " + ex.toString());
		}finally {
			DBCPConnectionManager2.close(conn, pstmt, rs);	
			
		}
		return aList;
	}
	
	public static long insert(long store_from, long id, Record record) {
		Connection conn = null;
		PreparedStatement pstmt = null;

		
		try {
			
			conn = DBCPConnectionManager2.getConnection();
			
			
				String sql = "insert into record (id, content, operation, created, store_to, store_from,  flag)values(?,?,?,?,?,?,?)";
				pstmt = conn.prepareStatement(sql);
					pstmt.setLong(1, getNextID()+1);
					
					pstmt.setString(2, record.getContent());
					pstmt.setString(3, record.operation);
					SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
					pstmt.setString(4, sft.format(new Date()));
					pstmt.setLong(5, id);					
					pstmt.setLong(6, store_from);
					pstmt.setString(7, "N");
					pstmt.executeUpdate();
					pstmt.getConnection().commit();
					
				
			
		} catch (SQLException ex) {
			try {
				conn.rollback();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return 0;
		} finally {
			DBCPConnectionManager2.close(conn, pstmt, null);			
		}
		return 1;
	}
	
	private static long getNextID() throws SQLException
	{
		long id = 0;
		Connection conn = null;
		// 加载Connector/J驱动
		try {
			conn = DBCPConnectionManager2.getConnection();
		}  catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// 执行SQL语句
		String sql = "select max(id) from record";
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try
		{
			pstmt = conn.prepareStatement(sql);			
			rs = pstmt.executeQuery();
			if (rs.next()) {
				id = rs.getLong(1);
			}
		}
		catch (Exception e)
		{
			
		}
		finally
		{
			DBCPConnectionManager2.close(conn, pstmt, rs);	
		}
		return id;
		
	}	//	fillCombos

	public List<Record> serverToClient(long client) {
		return returnToClient(client);
	}
	
	public boolean setFlag(List<Long> list) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			
			conn = DBCPConnectionManager2.getConnection();
			
			
				String sql = "update record set flag = 'Y' where id = ?";
				pstmt = conn.prepareStatement(sql);
				
				for(Long id : list) {
					pstmt.setLong(1, id);
					pstmt.addBatch();
				}
				pstmt.executeBatch();
				pstmt.getConnection().commit();
					
				
			
		} catch (SQLException ex) {
			System.out.println("ErrorServer : " + ex.toString());
		} finally {
			DBCPConnectionManager2.close(conn, pstmt, rs);	
			
		}
		
		return true;
	}
	
	
}
